//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
/************************************************************************/
/*                                                                      */
/*                 TrueFFS Software Development Kit                     */
/*              Copyright (C) M-Systems Ltd. 1995-2005                  */
/*                                                                      */
/************************************************************************/

/*-----------------------------------------------------------------------*/
/* Compilation flags for supported DiskOnChip devices                    */
/*                                                                       */
/* #define FL_G4_BASED_DISKONCHIP                                        */
/* #define FL_G3P3_BASED_DISKONCHIP - Add G3/P3 family support           */
/* #define FL_H1_BASED_DISKONCHIP   - Add H1 family support              */
/* #define FL_RAM_DISKONCHIP        - Add File MTD support               */
/* It is customary to define these flags in the project settings         */
/*-----------------------------------------------------------------------*/

#include "flsystem.h"
#include "flstdcmp.h"

#ifdef FL_RAM_DISKONCHIP
#include "rammtd.h"
#endif /* FL_RAM_DISKONCHIP */

/* environment variables */
#ifdef FL_ENVIRONMENT_VARS

unsigned char flUse8Bit;

/*-----------------------------------------------------------------------*/
/*                 f l s e t E n v V a r                                 */
/*                                                                       */
/*  Sets the value of all environment variables                          */
/*  Parameters : None                                                    */
/*-----------------------------------------------------------------------*/
void flSetEnvVar(void)
{
    flUse8Bit               = 1;
}

#endif /* FL_ENVIRONMENT_VARS */

/*-----------------------------------------------------------------------*/
/*                f l R e g i s t e r C o m p o n e n t s                */
/*                                                                       */
/* Register socket, MTD and Translation Layer components for usage       */
/*                                                                       */
/* This function is called by TrueFFS once only, at initialization.      */
/*                                                                       */
/* Parameters:                                                           */
/*      None                                                             */
/*-----------------------------------------------------------------------*/

FLStatus flRegisterComponents(void)
{
    FLStatus status    = flAdapterNotFound;
    FLStatus status1   = flAdapterNotFound;
    FLDword  startAddr = FL_LOW_DOC_ADDRESS;
    FLDword  endAddr   = FL_HIGH_DOC_ADDRESS;

	/*****************************************/
    /* Registering socket and MTD interfaces */
	/*****************************************/

#ifdef FL_RAM_DISKONCHIP

    /* Define RAM MTD type and attributes */

    DBG_PRINT_WRN(FLZONE_FULL,"Searching for RAM based DiskOnChip.\r\n");

    RamMtdRegParams.bInitalized       = FALSE; /* Must be set to FALSE            */
	                                           /* Prevent using the same file on  */
											   /* different 'sockets'             */ 
    RamMtdRegParams.pRamMtdFileName   = NULL;  /* Name of the image file          */
    RamMtdRegParams. bPerformDownloadOnPowerUp = TRUE  /* Assume power up on init */

	/* In case you want to create a new file use this field to specifiy  */
    /* the device type.                                                  */

    /* RAM_MTD_UNKNOWN - Already existing file, or by file name          */
    /* DOC_G3_512Mb_BGA_TSOP    / DOC_G3_1Gb_BGA                         */
    /* DOC_P3_256Mb_BGA_TSOP    / DOC_G3_1Gb_BGA_CASCADE_2               */
    /* DOC_P3_256Mb_BGA_TSOP_LP / DOC_G3_512Mb_BGA_TSOP_LP               */
    /* DOC_H1_4Gb_BGA           / DOC_H1_8Gb_BGA                         */ 
    /* DOC_H1_8Gb_BGA_CASCADE_2 /                                        */

    RamMtdRegParams.ramSimulateDevice = DOC_H1_4Gb_BGA;

    status1 = flRegisterDOCRamSOC();
    if(status1 == flOK)
    {
        DBG_PRINT_WRN(FLZONE_FULL,"Found RAM-based DiskOnChip.\r\n");
        checkStatus(flRegisterDOCRAM());
        status = flOK;
    }
    else
    {      
        DBG_PRINT_WRN_PRM(FLZONE_FULL,(FLTXT("Did Not find RAM based DiskOnChip (search range %lx - %lx) with status %d\r\n"),startAddr,endAddr,status1));
        if(status != flOK)
            status = status1;
    }

#endif /* FL_RAM_DISKONCHIP */

#ifdef FL_G3P3_BASED_DISKONCHIP

    /* Register G3/P3/G3LP/P3LP DiskOnChip devices */

    DBG_PRINT_WRN(FLZONE_FULL,"Searching for G3/P3 based DiskOnChip\r\n");
    status1 = flRegisterDOCM512SOC(startAddr, endAddr);
    if(status1 == flOK)
    {
        DBG_PRINT_WRN(FLZONE_FULL,"Found G3P3-based DiskOnChip\r\n");
        checkStatus(flRegisterDOCM512());
        status = flOK;
    }
    else
    {
        DBG_PRINT_WRN_PRM(FLZONE_FULL,(FLTXT("Did Not find G3/P3 based DiskOnChip (search range %lx - %lx) with status %d\r\n"),startAddr,endAddr,status1));
        if(status != flOK)
            status = status1;
    }

#endif /* FL_G3P3_BASED_DISKONCHIP */

#ifdef FL_H1_BASED_DISKONCHIP

    /* Register H1 DiskOnChip devices */

    DBG_PRINT_WRN(FLZONE_FULL,"Searching for H1 based DiskOnChip\r\n");
    status1 = flRegisterDOCH1SOC(startAddr, endAddr);
    if(status1 == flOK)
    {
        DBG_PRINT_WRN(FLZONE_FULL,"Found H1-based DiskOnChip\r\n");
        checkStatus(flRegisterDOCH1());
        status = flOK;
    }
    else
    {
        DBG_PRINT_WRN_PRM(FLZONE_FULL,(FLTXT("Did Not find H1 based DiskOnChip (search range %lx - %lx) with status %d\r\n"),startAddr,endAddr,status1));
        if(status != flOK)
            status = status1;
    }
#endif /* FL_H1_BASED_DISKONCHIP */

#ifdef FL_G4_BASED_DISKONCHIP
    /* Register G4 DiskOnChip devices */
    DBG_PRINT_WRN(FLZONE_FULL,"Searching for G4 based DiskOnChip\r\n");
    status1 = flRegisterDOCG4SOC(startAddr, endAddr);
    if(status1 == flOK)
    {
        DBG_PRINT_WRN(FLZONE_FULL,"Found G4-based DiskOnChip\r\n");
        checkStatus(flRegisterDOCG4());
        status = flOK;
    }
    else
    {
        DBG_PRINT_WRN_PRM(FLZONE_FULL,(FLTXT("Did Not find G4 based DiskOnChip (search range %lx - %lx) with status %d\r\n"),startAddr,endAddr,status1));
        if(status != flOK)
            status = status1;
    }
#endif /* FL_G4_BASED_DISKONCHIP */
	/**********************************/
    /* Registering translation layers */
	/**********************************/

    if(status == flOK)
    {
        status1 = flRegisterSAFTL();   /* Register saftl.c */
        if(status1 == flOK)
        {
            DBG_PRINT_WRN(FLZONE_FULL,"SAFTL was registred to manage the flash.\r\n");
        }
        else
        {
            DBG_PRINT_WRN_PRM(FLZONE_FULL,(FLTXT("Failed registering SAFLT to manage the flash media with status %d\r\n"),status1));
        }
        return status1;
    }
    return status;
} /* flRegisterComponents */

